package com.example.module_lzq_haibathree.utils

import android.content.Context
import android.content.res.Configuration
import android.graphics.*
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import com.example.module_lzq_haibathree.R


/**
 * =====================================================
 * 作    者: Xiuz
 * 创建日期: 2020/7/30 17:09
 * 描    述:
 * =====================================================
 */
class SpiritLevelView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    View(context, attrs, defStyleAttr) {
    /**
     * 测量的宽高
     */
    private var mWidth = 0
    private var mHeight = 0

    /**
     * 背景图
     */
    private val backgroundSrc: Bitmap

    /**
     * 红色小圆点
     */
    private val spritLevelRedDot: Bitmap

    /**
     * 左边bar
     */
    private val leftBackgroundSrc: Bitmap

    /**
     * 底部bar
     */
    private val buttomBackgroundSrc: Bitmap

    /**
     * 绿色小圆点
     */
    private val spritLevelGreenDot: Bitmap
    private val mPaint: Paint

    /**
     * 屏幕高
     */
    private val mHeightPixels: Int

    /**
     * 屏幕宽
     */
    private val mWidthPixels: Int

    /**
     * 中间背景图矩形
     */
    private var mCenterRect: Rect? = null

    /**
     * 左边bar矩形
     */
    private var mLeftRect: Rect? = null

    /**
     * 底部bar矩形
     */
    private var mButtomRect: Rect? = null

    /**
     * 中心圆点x坐标
     */
    private var centerBubbleX = 0

    /**
     * 中心圆点y坐标
     */
    private var centerBubbleY = 0
    private val centerBubblexy = Point()
    private val leftBubblexy = Point()
    private val buttomBubblexy = Point()

    /**
     * 中心点矩形
     */
    private var mCenterBubbleRect: Rect? = null

    /**
     * 左边bar矩形
     */
    private var mLeftBubbleRect: Rect? = null

    /**
     * 底部bar矩形
     */
    private var mButtomBubbleRect: Rect? = null

    /**
     * 小圆点阴影大小
     */
    private val bubbleShadow: Float

    /**
     * 背景图外圈圆的半径-
     */
    private var radius = 0
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawBackground(canvas)
        drawBubble(canvas)
    }

    private fun drawBubble(canvas: Canvas) {
        canvas.save()
        //圆点宽
        val i = ((mLeftRect!!.right - mLeftRect!!.left) / 2 + bubbleShadow).toInt()
        if (null == mCenterBubbleRect) {
            mCenterBubbleRect = Rect()
        }
        mCenterBubbleRect!!.left = centerBubbleX - i
        mCenterBubbleRect!!.right = centerBubbleX + i
        mCenterBubbleRect!!.top = centerBubbleY - i
        mCenterBubbleRect!!.bottom = centerBubbleY + i
        if (centerBubbleX == centerBubblexy.x && centerBubbleY == centerBubblexy.y) {
            canvas.drawBitmap(spritLevelGreenDot, null, mCenterBubbleRect!!, mPaint)
        } else {
            canvas.drawBitmap(spritLevelRedDot, null, mCenterBubbleRect!!, mPaint)
        }
        if (null == mLeftBubbleRect) {
            mLeftBubbleRect = Rect()
        }
        mLeftBubbleRect!!.left = leftBubblexy.x - i
        mLeftBubbleRect!!.top = centerBubbleY - i
        mLeftBubbleRect!!.right = leftBubblexy.x + i
        mLeftBubbleRect!!.bottom = centerBubbleY + i
        if (centerBubbleY == leftBubblexy.y) {
            canvas.drawBitmap(spritLevelGreenDot, null, mLeftBubbleRect!!, mPaint)
        } else {
            canvas.drawBitmap(spritLevelRedDot, null, mLeftBubbleRect!!, mPaint)
        }
        if (null == mButtomBubbleRect) {
            mButtomBubbleRect = Rect()
        }
        mButtomBubbleRect!!.left = centerBubbleX - i
        mButtomBubbleRect!!.top = buttomBubblexy.y - i
        mButtomBubbleRect!!.right = centerBubbleX + i
        mButtomBubbleRect!!.bottom = buttomBubblexy.y + i
        if (centerBubbleX == buttomBubblexy.x) {
            canvas.drawBitmap(spritLevelGreenDot, null, mButtomBubbleRect!!, mPaint)
        } else {
            canvas.drawBitmap(spritLevelRedDot, null, mButtomBubbleRect!!, mPaint)
        }
        canvas.restore()
    }

    private fun drawBackground(canvas: Canvas) {
        canvas.save()
        if (null == mCenterRect) {
            mCenterRect = Rect()
            mCenterRect!!.left = dp2px(51f).toInt() * mWidth / mWidthPixels
            val i = mWidth - 2 * mCenterRect!!.left
            mCenterRect!!.right = mCenterRect!!.left + i
            mCenterRect!!.top = (mHeight - i) / 2
            mCenterRect!!.bottom = mCenterRect!!.top + i
            centerBubbleX = (mCenterRect!!.right - mCenterRect!!.left) / 2 + mCenterRect!!.left
            centerBubbleY = (mCenterRect!!.bottom - mCenterRect!!.top) / 2 + mCenterRect!!.top
            centerBubblexy.x = centerBubbleX
            centerBubblexy.y = centerBubbleY
            radius =
                ((mCenterRect!!.right - mCenterRect!!.left) / 2 - dp2px(26f) * mWidth / mWidthPixels).toInt()
        }
        canvas.drawBitmap(backgroundSrc, null, mCenterRect!!, mPaint)
        if (null == mLeftRect) {
            mLeftRect = Rect()
            mLeftRect!!.left = (dp2px(16f) * mWidth / mWidthPixels).toInt()
            mLeftRect!!.right = (mCenterRect!!.left - dp2px(10f) * mWidth / mWidthPixels).toInt()
            val height =
                (mLeftRect!!.right - mLeftRect!!.left) * leftBackgroundSrc.height / leftBackgroundSrc.width
            val i = (mCenterRect!!.bottom - mCenterRect!!.top - height) / 2
            mLeftRect!!.top = i + mCenterRect!!.top
            mLeftRect!!.bottom = mCenterRect!!.bottom - i
            leftBubblexy.x = (mLeftRect!!.right - mLeftRect!!.left) / 2 + mLeftRect!!.left
            leftBubblexy.y = centerBubbleY
        }
        canvas.drawBitmap(leftBackgroundSrc, null, mLeftRect!!, mPaint)
        if (null == mButtomRect) {
            mButtomRect = Rect()
            val i =
                (mCenterRect!!.right - mCenterRect!!.left - (mLeftRect!!.bottom - mLeftRect!!.top)) / 2
            mButtomRect!!.left = mCenterRect!!.left + i
            mButtomRect!!.right = mCenterRect!!.right - i
            mButtomRect!!.top =
                mCenterRect!!.bottom + (dp2px(30f) * mHeight / mHeightPixels).toInt()
            mButtomRect!!.bottom = mLeftRect!!.right - mLeftRect!!.left + mButtomRect!!.top
            buttomBubblexy.x = centerBubbleX
            buttomBubblexy.y = mButtomRect!!.top + (mButtomRect!!.bottom - mButtomRect!!.top) / 2
        }
        canvas.drawBitmap(buttomBackgroundSrc, null, mButtomRect!!, mPaint)
        canvas.restore()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = MeasureSpec.getSize(widthMeasureSpec)
        mHeight = MeasureSpec.getSize(heightMeasureSpec)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        mHeight = measuredHeight
        mWidth = measuredWidth
    }

    private fun dp2px(value: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, value,
            resources.displayMetrics
        )
    }

    fun updataView(pitch: Double,roll: Double) {
        if (mCenterRect == null || mCenterBubbleRect == null || mButtomBubbleRect == null || mButtomRect == null || mLeftBubbleRect == null || mLeftRect == null) return
        val yAngle = pitch
        val zAngle = roll
/*
        val yAngle = values[1]
        val zAngle = values[2]
*/
        val x =
            ((mCenterRect!!.right - mCenterRect!!.left - (mCenterBubbleRect!!.right - mCenterBubbleRect!!.left)) / 2 * zAngle / MAX_ANGLE).toInt()
        centerBubbleX = centerBubblexy.x + x
        val y =
            ((mCenterRect!!.bottom - mCenterRect!!.top - (mCenterBubbleRect!!.bottom - mCenterBubbleRect!!.top)) / 2 * yAngle / MAX_ANGLE).toInt()
        centerBubbleY = centerBubblexy.y + y
        if (centerBubbleX < mButtomRect!!.left) {
            centerBubbleX = mButtomRect!!.left
        } else if (centerBubbleX > mButtomRect!!.right) {
            centerBubbleX = mButtomRect!!.right
        }
        if (centerBubbleY < mLeftRect!!.top) {
            centerBubbleY = mLeftRect!!.top
        } else if (centerBubbleY > mLeftRect!!.bottom) {
            centerBubbleY = mLeftRect!!.bottom
        }

        postInvalidate()
    }

    companion object {
        private val TAG = SpiritLevelView::class.java.simpleName

        /**
         * 定义水平仪能处理的最大倾斜角度，超过该角度气泡直接位于边界
         */
        private const val MAX_ANGLE = 30
    }

    init {
        val displayMetrics = context.resources.displayMetrics
        mWidthPixels = displayMetrics.widthPixels
        mHeightPixels = displayMetrics.heightPixels
        backgroundSrc = BitmapFactory.decodeResource(resources, R.mipmap.icon_spirit_bg)
        buttomBackgroundSrc = BitmapFactory.decodeResource(
            resources,
            R.mipmap.icon_horizontal_spirit_level_bar
        )
        leftBackgroundSrc = BitmapFactory.decodeResource(
            resources,
            R.mipmap.icon_vertical_spirit_level_bar
        )
        spritLevelRedDot = BitmapFactory.decodeResource(
            resources,
            R.mipmap.icon_spirit_red_dot
        )
        spritLevelGreenDot = BitmapFactory.decodeResource(
            resources,
            R.mipmap.icon_spirit_green_dot
        )
        mPaint = Paint()
        mPaint.isAntiAlias = true
        bubbleShadow = dp2px(2f)
    }
}